package com.shop.service.impl;

import com.shop.mapper.LeavebillMapper;
import com.shop.pojo.Leavebill;
import com.shop.service.WorkFlowService;
import com.shop.utils.Constants;
import org.activiti.engine.*;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

@Service
    public class WorkFlowServiceImpl implements WorkFlowService {

        @Autowired
        private RepositoryService repositoryService;
        @Autowired
        private RuntimeService runtimeService;
        @Autowired
        private TaskService taskService;
        @Autowired
        private HistoryService historyService;
        @Autowired
        private FormService formService;

        @Autowired
        private LeavebillMapper leaveBillMapper;

    @Override
    public void saveProcess(String processName, InputStream in) {

        ZipInputStream zipInputStream = new ZipInputStream(in);

        this.repositoryService
            .createDeployment()
                .name(processName)
                .addZipInputStream(zipInputStream)
                .deploy();
    }

//    @Override
//    public void startProcess(String name) {
//
//        // 1.设置【流程变量】
//        Map<String, Object> hashMap = new HashMap<String, Object>();
//        hashMap.put("userId", name);
//
//        // 2.通过【Leave_Key】即 act_re_deployment.ID_ 启动【发布了的流程】
//        this.runtimeService
//                .startProcessInstanceByKey(Constants.Leave_KEY, hashMap);
//            // Constants.Leave_KEY == 流程图 Name_
//    }

    // 开始流程
    @Override
    public void startProcess(Long id, String name) {

        // 设置【流程变量】
        Map<String, Object> map = new HashMap<>();
        map.put("userId", name);

        /* 2.组装 BUSINESS_KEY + 用于startProcessInstanceByKey(1, 2, 3)
        *      == process_key + "." + leaveId
        *  BUSINESS_KEY 长这样 = leaveBillProcess.28
        *   */
        String BUSINESS_KEY = Constants.Leave_KEY + "." + id;
        map.put("objId", BUSINESS_KEY);

        // 1.process_key; 2.business_key; 3.map的流程变量
        this.runtimeService
                .startProcessInstanceByKey(Constants.Leave_KEY, BUSINESS_KEY, map);
    }

    @Override
    public List<Task> findTaskListByName(String name) {

        List<Task> list = this.taskService
                .createTaskQuery()
                .taskAssignee(name)
                .orderByTaskCreateTime()
                .desc()
                .list();

        return list;
    }

    // 根据 taskID -> PROC_INST_ID(ProcessInstanceId) -> act_hi_comment. 查询 批注信息
    @Override
    public List<Comment> findCommentListByTaskId(String taskId) {

        Task task = this.taskService
                .createTaskQuery()
                .taskId(taskId)
                .singleResult();

        List<Comment> processInstanceComments = this.taskService
                .getProcessInstanceComments(task.getProcessInstanceId());

        return processInstanceComments;
    }


    // 保存批注 & complete 任务
    @Override
    public void submitTask(long id, String taskId, String comment, String name) {

        // 1 获取Task  ->  act_ru_task.ID_
        Task task = this.taskService
                .createTaskQuery()
                .taskId(taskId)
                .singleResult();

        // 2 从 task 任务中, 获取【当前流程实例id】
        String processInstanceId = task.getProcessInstanceId();
            // act_ru_task.ID_ 对应的 第4个PROC_INST_ID_

        // 3 设置【批注人】
        Authentication.setAuthenticatedUserId(name);

        /* 4 【保存批注 act_hi_comment】
            ------------------
            *  第5行 TASK_ID_  第6行 PROC_INST_ID_  第8行 MESSAGE_
         * */
        this.taskService.addComment(taskId, processInstanceId, comment);

    // 5 更新一下【请假单】(如果【流程】已经结束, 需要更新为2)
        ProcessInstance pi = this.runtimeService
                .createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();

        if (pi == null) {
            // 1 先去查询【请假单】, 不然导致全部更新
            Leavebill leavebill = this.leaveBillMapper.selectByPrimaryKey(id);

            // 2 设置【流程状态】为【2 完成】
            leavebill.setState(2);

            // 3 更细数据库
            this.leaveBillMapper.updateByPrimaryKey(leavebill);
        }

    }





}
